package com.wyq.jpa.model;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by dujiajiyi on 13-12-9.
 */
@Entity
@Table(name="roles")
public class Role extends IdEntity implements java.io.Serializable {
    private String name;
    private String description;
    private Set<User> users=new HashSet<User>();
    private Set<Permission> permissions=new HashSet<Permission>();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    //@ManyToMany注释表示Role是多对多关系的一边，mappedBy属性定义了Role为双向关系的维护端
    //User表是关系的维护者，owner side，有主导权，它有个外键指向role表。
    @ManyToMany(mappedBy = "roles")
    public Set<User> getUsers() {
        return users;
    }

    public void setUsers(Set<User> users) {
        this.users = users;
    }

    @ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
    @JoinTable(name = "roles_permissions",
            joinColumns = { @JoinColumn(name = "role_id", referencedColumnName = "id") },
            inverseJoinColumns = { @JoinColumn(name = "permission_id", referencedColumnName = "id") })
    public Set<Permission> getPermissions() {
        return permissions;
    }

    public void setPermissions(Set<Permission> permissions) {
        this.permissions = permissions;
    }
}
